<template>
  <div class="app-container">
    <!-- 条件查询渲染 -->
    <div>
      <el-input v-model="pageDto.where.name" placeholder="请输入模型名称" style="width: 250px;margin-left: 20px;"></el-input>
      <el-input v-model="pageDto.where.tenantId" placeholder="请输入模型名称" style="width: 250px;margin-left: 20px;"></el-input>
      <el-input v-model="pageDto.where.productKey" placeholder="请输入模型名称"
        style="width: 250px;margin-left: 20px;"></el-input>
      <el-cascader v-model="pageDto.where.areaCode" placeholder="请选择行政区" :options="area"
        :props="{ expandTrigger: 'hover' }" style="width: 250px;margin-left: 20px;"></el-cascader>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="pageDto.now = 1;search()">搜索</el-button>
      <el-button type="danger" icon="el-icon-refresh" @click="reset()">清空</el-button>
    </div>

    <!-- 页面表格渲染 -->
    <div style="margin-top: 20px;">
      <el-table :data="productList" style="width: 100%;margin-bottom: 20px;"
        :header-cell-style="{ background: '#F7F7F8', color: '#18191B', 'text-align': 'center' }">
        <el-table-column prop="name" label="产品名称" align="center"> </el-table-column>
        <el-table-column label="所属租户" align="center">
          <!-- 自定义渲染 -->
          <template slot-scope="scope">
            <el-tag key="所属租户" type="success">
              {{ scope.row.tenantName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="行政区" align="center">
          <template slot-scope="scope">
            <el-cascader :value="scope.row.areaCode" :options="area" disabled
              style="width: 250px;margin-left: 20px;"></el-cascader>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="用户名" align="center"></el-table-column>
        <el-table-column prop="productKey" label="产品表示Key" align="center"></el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageDto.now"
        :page-sizes="[5, 10, 50, 100]" :page-size="pageDto.size" layout="total, sizes, prev, pager, next, jumper"
        :total="total" style="text-align: center;">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { page, findAll } from "@/api/product"
export default {
  data() {
    return {
      //省市区三级联动数据
      area: [],
      //表格数据
      productList: [],
      pageDto: {
        now: 1,
        size: 5,
        where: {}
      },
      pages: 0,
      total: 0,

    }
  },
  methods: {
    //查询页面所有数据
    search() {
      if(this.pageDto.where.areaCode){
        this.pageDto.where.areaCode = this.pageDto.where.areaCode.toString();
      }
      page(this.pageDto).then((res) => {
        this.productList = res.list;
        for (var i = 0; i < this.productList.length; i++) {
          this.productList[i].areaCode = this.areacode(this.productList[i].areaCode)
        }
        this.pages = res.pages;
        this.total = res.total;
      });
      findAll().then((res) => {
        this.area = res.data
        var children = new Array()
        for (var i = 0; i < this.area.length; i++) {
          if (this.area[i].children.length < 1) {
            // children若为空数组，则将children设为undefined
            this.area[i].children = undefined;
          } else {
            children = this.area[i].children
            for (var j = 0; j < children.length; j++) {
              if (children[j].children.length < 1) {
                // children若为空数组，则将children设为undefined
                children[j].children = undefined;
              }
            }
            this.area[i].children = children;
            children = []
          }
        }
      })
    },

    //分页页面显示数据多少
    handleSizeChange(size) {
      this.pageDto.size = size;
      this.search();
    },

    //分页页面当前页
    handleCurrentChange(now) {
      this.pageDto.now = now;
      this.search();
    },

    //转换areaCode的值
    areacode(data) {
      var areaData = new Array()
      areaData = data.split("-")
      for (var i = 0; i < areaData.length; i++) {
        areaData[i] = parseInt(areaData[i])
      }
      return areaData
    },

    reset(){
      this.pageDto.where = {}
    },

    codearea(){
      

    }

  },
  created() {
    this.search()
  }
}
</script>

<style>

</style>
